﻿using System;

namespace Gadgeteer.Interfaces.Sample.Safe.SafeApp
{
    public class Lock : LockBase, ILock
    {
        private readonly int[] code;

        public Lock(int[] code)
        {
            if (code == null || code.Length == 0)
                throw new ArgumentNullException("code");

            this.code = code;
            IsOpen = false;
        }

        public bool OpenWithCode(int[] openWith)
        {
            if (openWith != null && code.Length == openWith.Length)
            {
                bool succes = true;
                for (int i = 0; i < code.Length; i++)
                {
                    succes &= (code[i] == openWith[i]);
                }

                if (succes)
                {
                    IsOpen = true;
                    return true;
                }
            }

            Close();
            return false;
        }
    }
}
